'''
Created on 05-08-2010

@author: polo
'''

import pygame,random
import scenes

class Floor(object):

	def __init__(self, screen):
		''' x,altura, largo, separacion'''
		self.floors = []
		self.blocks = [
		    pygame.image.load("images/blck_start.png"),
		    pygame.image.load("images/blck1.png"),
            pygame.image.load("images/blck2.png")
		]
		self.scenes = scenes.Scenes(screen)
		self.color = (255,255,255)
		self.RIGHT_LIMIT = screen.get_width()
		self.i=0
		self.SPEED=10
		self.ACEL=.02
		self.DISTANCE=0
		self.pushNew(0)
		self.current_scene=0 ## wood
		self.next_scene=0    ## wood
		self.DIED_LVL=300+140
		
	def pushEnd(self,limit):
##		print "creando nuevo"
		i=0
		offset=self.scenes.getOffset(self.SPEED)
		high=self.scenes.HIGH_START
		self.floors.append([limit,high,self.blocks[i].get_width(),offset,self.scenes.block_end])
	def pushNew(self,limit):
##		print "creando nuevo"

		offset=self.scenes.getOffset(self.SPEED)
		high=self.scenes.getHigh()
		block=self.scenes.getBlockLoop()
		self.floors.append([limit,
						high,
						block.get_width(),
						offset,
						block])
	def popNew(self):
##		print "sacando nuevo"
		self.floors.pop(0)
	def getStart(self,floor):
		floor[0]-=self.SPEED
		print floor[1]
		return (floor[0],floor[1])
	def getEnd(self,floor):
		return (floor[0]+floor[2],floor[1])
	def draw(self,screen):

		def texto(texto, posx, posy, color=(255, 255, 255)):
			fuente = pygame.font.Font("images/DroidSans.ttf", 25)
			salida = pygame.font.Font.render(fuente, texto, 1, color)
			salida_rect = salida.get_rect()
			salida_rect.x = posx
			salida_rect.y = posy
			return salida, salida_rect

		for floor in self.floors:			
			screen.blit(floor[4],self.getStart(floor))
			
		p_speed, p_speed_rect = texto("Speed:"+str(self.SPEED), 10, 40)
		p_meters, p_meters_rect = texto("Distance:"+str(self.DISTANCE), 10, 100)
		p_acel, p_acel_rect = texto("Acel:"+str(self.ACEL), 10, 160)
		screen.blit(p_speed, p_speed_rect)
		screen.blit(p_meters, p_meters_rect)
		screen.blit(p_acel, p_acel_rect)
		if (self.floors[-1][0]+self.floors[-1][2]+self.floors[-1][3]<self.RIGHT_LIMIT):
			limit=self.floors[-1][0]+self.floors[-1][2]+self.floors[-1][3]
			if self.scenes.status==self.scenes.STATUS_END:
				self.current_scene=self.next_scene
				self.scenes.changeScene(self.current_scene)				
				self.scenes.status=self.scenes.STATUS_START
			elif self.scenes.status==self.scenes.STATUS_START:		
				self.scenes.status=self.scenes.STATUS_LOOP
			elif self.scenes.status==self.scenes.STATUS_LOOP:
				self.next_scene=self.getNewScene()
				if (self.next_scene!=self.current_scene):
					self.scenes.status=self.scenes.STATUS_END				
			self.pushNew(limit)
		if (self.floors[0][0]+self.floors[0][2]<0):
			self.popNew()
		self.SPEED+=self.ACEL
		self.DISTANCE+=self.SPEED/2
	def getNewScene(self):
		if self.DISTANCE<1000:
			return 0
		else:
			return 1
	def floorY(self, x):
		for floor in self.floors:			
			if (floor[0]<x and floor[0]+floor[2]>x):		
				return floor[1]+140
		return self.DIED_LVL
	def stopAll(self):
		self.SPEED=0
		self.ACEL=0
	def restart(self):
		self.SPEED=8
		self.ACEL=.02
	def slowdown(self):
		self.SPEED*=.9
		self.ACEL+=.001